package com.chinamsp.dydb.query;

import java.io.Serializable;

import com.alibaba.druid.util.StringUtils;
import com.chinamsp.dydb.convert.ORMNameConverter;

/**
 * 单个条件类型
 * @author shifenghu
 *
 */
public class SingleFieldCondition extends Condition implements Serializable{
	private static final long serialVersionUID = -4466041047921086476L;
	
	/**
	 * 字段名称
	 */
	private String field;
	/**
	 * 原始值
	 */
	private Object value;
	/**
	 * 转换后的sql 值
	 */
	private Object sqlValue;
	
	/**
	 * sql 操作符
	 */
	private String sqlOperator;
	
	/**
	 * 原始操作符
	 */
	private SymbolType operator;
	
	/**
	 * 字段类型
	 */
	private FieldType type;
	
	@Override
	public SymbolType getOperator() {
		return operator;
	}
	@Override
	public void setOperator(SymbolType operator) {
		this.operator = operator;
	}
	@Override
	public String getField() {
		return field;
	}
	@Override
	public void setField(String field) {
		this.field = field;
	}
	@Override
	public Object getValue() {
		return value;
	}
	@Override
	public FieldType getType() {
		return type;
	}
	@Override
	public void setType(FieldType type) {
		this.type = type;
	}
	@Override
	public Object getSqlValue() {
		return sqlValue;
	}
	
	@Override
	public String getSqlOperator() {
		return sqlOperator;
	}
	

	/**
	 * 
	 * 设置value值
	 */
	@Override
	public void setValue(Object value)  {
		this.value = value;
		sqlValue = operator.getValueConverter().convert(field, type, operator, value);
	}
	
	@Override
	protected void parse(String parameter) {
		String[] array = parameter.split(Condition.FIELD_INTERNAL_DELIMETER);
		String field = array[0];
		setField(ORMNameConverter.INSTANCE.convertToField(field));
		
		//只有一个参数(字符串eq查询) 字段
		if(array.length == 1) {
			setType(FieldType.STRING);
			setOperator(SymbolType.EQ);
		}else	if(array.length == 2) {  //只有二个参数  字段_操作符/字段_数据类型   
			//第2个是操作符，则默认是字符类型
			if(SymbolType.byType(array[1]) != null) {
				setOperator(SymbolType.byType(array[1]));
				setType(FieldType.STRING);
			}else{	//字段_数据类型
				setOperator(SymbolType.EQ);
				setType(FieldType.byType(array[1]));
			}
		}else if(array.length == 3) { //有三个参数(非字符串查询)   字段名_类型_操作符
			setType(FieldType.byType(array[1]));
			setOperator(SymbolType.byType(array[2]));
		}
		else if(array.length == 4){ // 四个参数 字段名_类型_操作符_是否字段转换
			setType(FieldType.byType(array[1]));
			setOperator(SymbolType.byType(array[2]));
			if("0".equalsIgnoreCase(array[3])){
				setField(field);
			}
		}
		// 获取 operator
		sqlOperator = operator.getExpCoverter().convert(field, type, operator);
	}
	
	
	
	@Override
	public String toString() {
		return "SingleFieldCondition [field=" + field + ", value=" + value + ", sqlValue=" + sqlValue + ", sqlOperator="
				+ sqlOperator + ", operator=" + operator + ", type=" + type + "]";
	}
	
	@Override
	public boolean isIgnore() {
		return StringUtils.isEmpty(field) || StringUtils.isEmpty(sqlOperator) || (operator != SymbolType.NOT_NULL && operator != SymbolType.IS_NULL && value == null);
	}
	
}
